﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour,IDamageble
{

    EnemyBaseState currentState;

    public Animator anim;
    private GameObject alarmSign;
    public int animState;

    public List<Enemy> enemyList;

    [Header("基础状态")]
    public float health;
    public bool isDead;
    public bool hasBomb;
    public bool isBoss;
    public bool isEnemy;

    [Header("移动配置")]
    public float speed;
    public Transform pointA, pointB;
    public Transform targetPoint;

    [Header("攻击配置")]
    private float nextAttack = 0;
    public float attackRate, attackRange, skillRange;


    public List<Transform> attackList = new List<Transform>();

    public PatrolState patrolState = new PatrolState();
    public AttackState attackState = new AttackState();
    // Start is called before the first frame update

    public virtual void Init()
    {
        anim = GetComponent<Animator>();
        //获取子组件
        alarmSign = transform.GetChild(0).gameObject;
    }

    //Awake 生命周期优先于Start 进行调用
    public void Awake()
    {
        Init();
    }

    void Start()
    {
        enemyList.Add(this);
        TransitionToState(patrolState);
        if (isBoss)
        {
            UIManager.instance.healthBar.SetActive(true);
            UIManager.instance.SetBossHealth(health);
        }
    }

    // Update is called once per frame
    public virtual void Update()
    {
        anim.SetBool("dead", isDead);
        if (isDead)
        {
            return;
        }
        currentState.OnUpdate(this);
        anim.SetInteger("state", animState);

        // if(isBoss){
        //     UIManager.instance.UpdateBossHealth(health);
        // }
    }

    public virtual void GetHit(float damage)
    {
        health -= damage;
        if (isBoss)
        {
            UIManager.instance.UpdateBossHealth(health);
        }
        if (health < 1)
        {
            health = 0;
            isDead = true;
            enemyList.Remove(this);
        }
        else
        {
            anim.SetTrigger("hit");
        }
    }


    public void TransitionToState(EnemyBaseState state)
    {
        currentState = state;
        currentState.EnterState(this);
    }


    public void MoveToTarget()
    {
        transform.position = Vector2.MoveTowards(transform.position, targetPoint.position, speed * Time.deltaTime);
        FilpDirection();
    }

    //攻击
    public void AttackAction()
    {
        if (Vector2.Distance(transform.position, targetPoint.position) < attackRange)
        {
            if (Time.time > nextAttack)
            {
                anim.SetTrigger("attack");
                nextAttack = Time.time + attackRate;
            }
        }
    }

    //技能
    public virtual void SkillAction()
    {
        if (Vector2.Distance(transform.position, targetPoint.position) < skillRange)
        {
            if (Time.time > nextAttack)
            {
                anim.SetTrigger("skill");
                nextAttack = Time.time + attackRate;
            }
        }
    }

    //日白
    public void Dead()
    {
        gameObject.SetActive(false);
    }

    //翻转方向
    public void FilpDirection()
    {
        if (transform.position.x < targetPoint.position.x)
        {
            //人物旋转属性 rotation 需要使用 Quaternion Euler来设置值
            transform.rotation = Quaternion.Euler(0f, 0f, 0f);
        }
        else
        {
            //人物旋转属性 rotation 需要使用 Quaternion Euler来设置值
            transform.rotation = Quaternion.Euler(0f, 180f, 0f);
        }
    }

    public void SwitchPoint()
    {
        //巡航点的坐标无法和人物坐标相等，只能判断趋近于巡航点
        //如果A点 减去 人物坐标的绝对值 大于于 B点 减去人物坐标的绝对值， 说明人物在A点
        if (Mathf.Abs(pointA.position.x - transform.position.x) > Mathf.Abs(pointB.position.x - transform.position.x))
        {
            targetPoint = pointA;
        }
        else
        {
            targetPoint = pointB;
        }
    }

    //unity 自带，检测是否在碰撞体内，Stay持续检测
    public void OnTriggerStay2D(Collider2D collision)
    {
        if (!attackList.Contains(collision.transform) && !hasBomb && !GameManager.instance.gameOver)  //判断是否存在同一个碰撞体
        {
            attackList.Add(collision.transform);
        }
    }
    //Enter 进入检测
    public void OnTriggerEnter2D(Collider2D collision)
    {
        if (!isDead && !anim.GetCurrentAnimatorStateInfo(1).IsName("attack") && !GameManager.instance.gameOver)
        {
            StartCoroutine(OnAlarm());
        }
    }

    //Exit 退出检测
    public void OnTriggerExit2D(Collider2D collision)
    {
        attackList.Remove(collision.transform);
    }

    //协程
    IEnumerator OnAlarm()
    {  //控制警报信号
        alarmSign.SetActive(true);
        //等待执行时常,获取动画执行长度
        yield return new WaitForSeconds(alarmSign.GetComponent<Animator>().GetCurrentAnimatorClipInfo(0)[0].clip.length);
        alarmSign.SetActive(false);
    }
}